<?php
namespace App\Model\Account;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class Admin extends Model
{
    protected $table = 'admin';
    protected $request;
    //列表
    public static function adminList($pageLimit, $where = [])
    {
        $list = DB::table('admin')
            ->where($where)
            ->select('a_id', 'a_user_name', 'a_account', 'a_sex', 'a_phone', 'a_emergency', 'a_emergency_phone',
                'a_address', 'a_native_place', 'a_status',  DB::raw('from_unixtime(a_create_time) AS a_create_time'),
                DB::raw('from_unixtime(a_update_time) AS a_update_time'))
            ->paginate($pageLimit);
        if (sizeof($list) == 0){
            return false;
        }
        return $list;
    }
    //添加
    public static function addAdmin($params)
    {
        $aId = DB::table('admin')->where('a_account', $params['a_account'])->value('a_id');
        if ($aId) {
            return -1;
        }
        return  DB::table('admin')->insertGetId([
            'a_account'          =>    $params['a_account'],
            'a_password'         =>    Hash::make('123456'),
            'a_user_name'        =>    $params['a_user_name'],
            'a_sex'              =>    $params['a_sex'],
            'a_phone'            =>    $params['a_phone'],
            'a_emergency'        =>    $params['a_emergency'],
            'a_emergency_phone'  =>    $params['a_emergency_phone'],
            'a_address'          =>    $params['a_address'],
            'a_native_place'     =>    $params['a_native_place'],
            'a_status'           =>    $params['a_status'],
            'a_create_time'      =>    time(),
            'a_update_time'      =>    time()
        ]);
    }
    //修改
    public static function adminEdit($params)
    {
        return DB::table('admin')->where('a_id', $params['a_id'])->update([
            'a_user_name'        =>    $params['a_user_name'],
            'a_sex'              =>    $params['a_sex'],
            'a_phone'            =>    $params['a_phone'],
            'a_emergency'        =>    $params['a_emergency'],
            'a_emergency_phone'  =>    $params['a_emergency_phone'],
            'a_address'          =>    $params['a_address'],
            'a_native_place'     =>    $params['a_native_place'],
            'a_status'           =>    $params['a_status'],
            'a_update_time'      =>    time()
        ]);
    }

    //修改禁用 启用状态
    public static function updateStatus($params)
    {
        $aId = $params['a_id'];
        $status = $params['a_status'];
        if ($status == 1) {
            $aStatus = 2;
        } elseif($status == 2){
            $aStatus = 1;
        }else{
            return false;
        }
        return DB::table('admin')->where('a_id', $aId)->update([
            'a_status'       => $aStatus,
            'a_update_time'  => time()
        ]);
    }

    //修改密码
    public static function updatePass($params)
    {
        if ($params['a_password_again'] == $params['a_password']){
            $oldPass = DB::table('admin')->where('a_id', $params['a_id'])->value('a_password');
            if (Hash::check($params['oldpass'], $oldPass)){
                return DB::table('admin')->where('a_id', $params['a_id'])->update(['a_password' => Hash::make($params['a_password']), 'a_update_time' => time()]);
            } else {
                //旧密码输入错误
                return -2;
            }
        } else {
            //新密码两次输入不一致
            return -1;
        }
    }
    //登录
    public static function login($params)
    {
        $password = DB::table('admin')->where('a_account', $params['a_account'])->value('a_password');
        if (!$password) {
            return -1;
        }
        if (Hash::check($params['a_password'], $password)){
            $token = str_random(60);
            DB::table('admin')->where('a_account', $params['a_account'])->update(['a_token' => $token, 'a_update_time' => time()]);
            $aId = DB::table('admin')->where('a_token', $token)->value('a_id');
            Cache::put($token, $aId, 3600);
            return $token;
        } else {
            return false;
        }
    }


}